<template>
  <el-upload
    :class="[
      'upload_one_container_max',
      { file_upload_content: fileType != 1 && fileType != 5 }
    ]"
    v-bind="$attrs"
    :action="uploadUrl"
    :disabled="disabled"
    :show-file-list="isShowFileList"
    :accept="fileAccept"
    :file-list="showFilesList"
    :on-success="handleOnSuccess"
    :before-upload="beforeUpload"
  >
    <div v-if="fileType != 1 && fileType != 5" class="file_upload_but">
      <div v-if="uploadSlot">
        <slot></slot>
      </div>
      <el-button
        v-if="!uploadSlot"
        size="small"
        type="primary"
        style="margin-right: 15px"
        >点击上传</el-button
      >
      <el-button
        v-if="fileUrl && showDel"
        circle
        type="danger"
        icon="el-icon-delete"
        @click.stop="handleOnRemove"
      ></el-button>
    </div>
    <div v-if="fileUrl && showFile">
      <el-image
        fit="cover"
        :style="`width:${width};height:${height};`"
        :src="fileUrl"
      ></el-image>
      <audio controls v-if="fileType == 2">
        <source :src="fileUrl" />
        您的浏览器不支持 audio 元素。
      </audio>
      <video controls v-if="fileType == 3">
        <source :src="fileUrl" />
        您的浏览器不支持 video 标签。
      </video>
      <ul v-if="fileType == 4" class="el-upload-list el-upload-list--text">
        <li tabindex="0" class="el-upload-list__item is-success">
          <a class="el-upload-list__item-name">
            <i class="el-icon-document"></i>{{ fileUrl }}
          </a>
          <label class="el-upload-list__item-status-label">
            <i class="el-icon-upload-success el-icon-circle-check"></i>
          </label>
          <i class="el-icon-close" @click.stop="handleOnRemove"></i>
        </li>
      </ul>
    </div>
    <div v-else>
      <i
        v-if="fileType == 1 || fileType == 5"
        class="el-icon-plus avatar-uploader-icon"
        :style="`width:${width};height:${height};line-height:${height};`"
      ></i>
    </div>
    <div slot="tip" class="el-upload__tip">
      {{ proposal }}请上传{{ fileAccept.replace(/,/g, '/') }}文件，且不超过{{
        maxSize == 0.195 ? '200KB' : `${maxSize}MB`
      }}
    </div>
  </el-upload>
</template>

<script>
// 文件单个上传组件
import config from './config'
export default {
  props: {
    // 父组件的 v-model 值
    value: {
      type: String,
      default: ''
    },
    // 文件大小限制
    maxSize: {
      type: Number,
      default: config.imgMaxSize
    },
    // 上传的文件类型[1 图片][2 音频][3 视频][4 其他]
    fileType: {
      type: Number,
      default: 1
    },
    // 是否自定义上传按钮样式
    uploadSlot: {
      type: Boolean,
      default: false
    },
    // 是否显示上传列表
    isShowFileList: {
      type: Boolean,
      default: false
    },
    // 上传文件类型
    accept: {
      type: String,
      default: ''
    },
    // 上传链接
    uploadUrl: {
      type: String,
      default: config.uploadUrl
    },
    // 上传建议
    proposal: {
      type: String,
      default: ''
    },
    // 是否显示删除按钮
    showDel: {
      type: Boolean,
      default: true
    },
    // 是否显示删除按钮
    showFile: {
      type: Boolean,
      default: true
    },
    // 是否本地上传文件
    isLocalFile: {
      type: Boolean,
      default: false
    },
    //是否能继续上传
    disabled: {
      type: Boolean,
      default: false
    },
    // 回显文件
    filesList: {
      type: Array,
      default: () => {
        return []
      }
    },
    width: {
      type: String,
      default: config.width
    },
    height: {
      type: String,
      default: config.height
    }
  },
  name: 'UploadOne',
  data() {
    return {
      fileUrl: '',
      showFilesList: [],
      isHandle: false,
      fileAccept: config.acceptType[this.fileType]
    }
  },
  watch: {
    value(val) {
      this.fileUrl = val // 同步子组件的值
    },
    filesList(val) {
      this.showFilesList = val
    }
  },
  mounted() {
    if (this.accept) {
      this.fileAccept = this.accept
    }
  },
  methods: {
    // 上传成功
    handleOnSuccess(res, file, fileList) {
      this.fileUrl = '' // 重绘资源显示
      this.$nextTick(() => {
        this.$emit('input', res.data)
        this.$emit('change', res, file, fileList)
      })
    },
    // 文件删除
    handleOnRemove(file, fileList) {
      this.$emit('input', '')
      this.$emit('reChange', file, fileList)
    },
    // 上传前限制
    beforeUpload(file) {
      // console.log(file)
      const isSize = file.size / 1024 / 1024 < this.maxSize
      const fileName = file.name
      let extension = fileName.substring(file.name.lastIndexOf('.') + 1)
      // console.log(extension,"*****");
      if (
        extension !== 'png' &&
        extension !== 'jpg' &&
        extension !== 'JPG' &&
        extension !== 'PNG' &&
        extension !== 'JPEG' &&
        extension !== 'jpeg'
      ) {
        this.$message.error(
          '只能上传后缀是.png或.jpg或.jpeg的文件,请重新上传正确的附件'
        )
      }
      if (!isSize) {
        if (this.maxSize == 0.195) {
          this.$message.error(`上传文件大小不能超过 200KB!`)
        } else {
          this.$message.error(`上传文件大小不能超过 ${this.maxSize}Mb!`)
        }
      }
      this.$emit('beforeUpload', file)
      return isSize && !this.isLocalFile
    }
  }
}
</script>

<style lang="less" scoped>
.file_upload_content,
.upload_one_container_max {
  .file_upload_but {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 20px;
  }
  video {
    max-width: 100%;
    max-height: 100%;
  }
  /deep/ .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
  }
}
.file_upload_content {
  /deep/ .el-upload {
    border: none;
  }
}
</style>
